<?php
require_once "BaseAction.php";
require_once 'lib/thrift/client/BlogService.php';

class Event_BlogController extends TTEvent_Base_Action 
{
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event-detail');
		$this->_helper->eventCommon->buildMainMenuBlock();
	}
	
	// blog list
	public function indexAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
		if(!$this->_helper->privacy->isEnable('blog', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
        
        $this->setTitle("Blog | ".$event->event_name);
		
		
		$user = $this->getUser();
		$req = $this->getRequest();
		
		
		if ($req->isPost())
		{
			$blog_delete = $req->getPost('blog_delete', 0);
			$nids = $req->getPost('nids', array());
			if ($blog_delete==1)
			{
				if(!empty($nids)){
					$this->delEventBlogs($eid, $nids);
					$changed = array('changed'=>time(), 'blog_count'=>$this->getEventBlogCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->removeSuccess = 1;
					$this->view->removeCount = count($nids);
				}
			}
		}
		$page = $req->getQuery('page', 0);
		$limit = 20;
		
		$total = $this->getEventBlogCount($eid);
		$totalPage = ceil($total/$limit);
		if($page >= $totalPage)
			$page = $totalPage-1;
		$eventBlogs = $this->getEventBlogs($eid, $page+1, $limit);
		
		
		$this->view->blogList = $eventBlogs;
		$this->view->totalBlog = $total;
		$this->view->limit = $limit;
		$this->view->user = $user;
		//$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/blog/" . $event->eid, "param" => "", 'haveNext'=>(count($eventBlogs)>$limit)));
		
		$pageInfo = array();
		$pageInfo['total_item'] = $total;
		$pageInfo['page'] = $page;
		$pageInfo['total_page'] = $totalPage;
		$this->view->pager = $this->view->partial("pager.phtml", array('pageInfo'=>$pageInfo, 'ext_param'=>"", "url" => "/event/blog/" . $event->eid, "param" => "", 'title'=>'blog'));
		$this->view->event = $event;
		$this->view->page = $page;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
		
	}
	
	public function delEventBlogs($eid, $nids){
		$eid = (int)$eid;
		if($eid && is_array($nids) && count($nids)>0){
			$success = false;
			try {
				$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->removeEventBlogs($eid, $nids);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				$success = false;
				//$blogs = array();
			}
			if($success){
				$this->clearEventBlogCache($eid);
			}
		}
	}
	
	public function addEventBlogs($eid, $nids, $uid){
		
		$eid = (int)$eid;
		if($eid && is_array($nids) && count($nids)>0){
			$success = false;
			try {
				$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
				$v_client->OpenTransport();
				$v_client->client()->addEventBlogs($eid, $nids, $uid);
				$v_client->CloseTransport();
				$success = true;
			} catch (Exception $ex){
				$success = false;
				//$blogs = array();
			}
			if($success){
				$this->clearEventBlogCache($eid);
			}
		}
	}
	
	
	public function addAction(){
		$eid = (int)$this->_getParam('eid', 0);
		if(!$eid)
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		
		$event = $this->_helper->modelLoader->getEventInfo($eid);
		if(empty($event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event');
		}
        
        $this->setTitle("Thêm blog | ".$event->event_name);
		$user = $this->getUser();
		if(!$this->_helper->privacy->isCanAdd('blog', $eid, $user->uid)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		
		if(!$this->_helper->privacy->isEnable('blog', $event)){
			$this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/detail/'.$eid);
		}
		
		$req = $this->getRequest();
		
		if ($req->isPost())
		{
			$nids = $req->getPost('nids', array());
			$blog_add = $req->getPost('blog_add', 0);
			$nids = $req->getPost('nids', array());
			
			if ($blog_add==1)
			{
				if(!empty($nids)){
					$this->addEventBlogs($eid, $nids, $user->uid);
					$changed = array('changed'=>time(), 'blog_count'=>$this->getEventBlogCount($eid));
					$this->_helper->modelLoader->changeEventInfo($eid, $changed);
					$this->view->addSuccess = 1;
					$this->view->addCount = count($nids);
				}
			}
		}
		$page = $req->getQuery('page', 0);
		$limit = 20;
		
		
		$total  = $this->getUserBlogCount($user->uid);
		$eventBlogs = $this->getEventUserBlog($user->uid, $eid, $page+1, $limit);
		
		$this->view->blogList = $eventBlogs;
		$this->view->limit = $limit;
		$this->view->totalBlog = $total;
		$this->view->uid = $user->uid;
		$this->view->user = $user;
		//$this->view->pager = $this->view->partial("pager-new.phtml", array('page'=>$page, "url" => "/event/blog/add/" . $event->eid, "param" => "", 'haveNext'=>(count($eventBlogs)>$limit)));
		
		$pageInfo = array();
		$pageInfo['total_item'] = $total;
		$pageInfo['page'] = $page;
		$pageInfo['total_page'] = ceil($total/$limit);
		$this->view->pager = $this->view->partial("pager.phtml", array('pageInfo'=>$pageInfo, 'ext_param'=>"", "url" => "/event/blog/add/" . $event->eid, "param" => "", 'title'=>'blog'));
		$this->view->event = $event;
		
		$this->view->isAdmin  = $this->_helper->modelLoader->isEventAdmin($eid, $user->uid);
		$this->view->action = 'list';
		$this->view->page = $page;
		$this->view->htmlLeftCol = $this->_helper->eventCommon->buildHtmlEventLeftCol($eid);
	}
	
	public function getEventBlogCount($eid){
		
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getEventBlogCount($eid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$blogs = array();
		}
		
		return $total;
		
	}
	
	
	public function getUserBlogCount($eid){
		
		$v_client = null;
		$total = 0;
		
		try {
			$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
			$v_client->OpenTransport();
			$total = $v_client->client()->getUserBlogCount($eid);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			//$blogs = array();
		}
		
		return $total;
		
	}
	
	public function getEventBlogs($eid, $page=1, $limit=20){

		$v_client = null;
		$eventBlogs = array();
		
		$eid = (int)$eid;
		$page = (int)$page;
		$limit = (int)$limit;
		
		try {
			$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventBlogs = $v_client->client()->getEventBlog($eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			
		}
		return $eventBlogs;
		
	}
	
	public function getEventUserBlog($uid, $eid, $page, $limit){
		
		$v_client = null;
		$eventBlogs = array();
		$uid = (int)$uid;
		$eid = (int)$eid;
		$page = (int)$page;
		$limit = (int)$limit;
		
		try {
			$v_client = new BlogService(BLOG_THRIFT_SERVER, BLOG_THRIFT_PORT);
			$v_client->OpenTransport();
			$eventBlogs = $v_client->client()->getUserEventBlog($uid, $eid, $page, $limit);
			$v_client->CloseTransport();
		} catch (Exception $ex){
			
		}
		return $eventBlogs;

		
	}
	
	
	
	private function tmpBlogs(){
		$ret = array();
		
		$blog = new stdClass();
		$blog->nid = 110963;
		$blog->uid = 448;
		$blog->uname = 'sonlecong';
		$blog->title = 'Test blog của Hưng Nguyễn';
		$blog->teaser = "Test blog của Hưng NguyễnTest blog của Hưng NguyễnTest blog của Hưng Nguyễn";
		$blog->body = 'Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn Test blog của Hưng Nguyễn ';
		$blog->status = 1;
		$blog->created = 1216805262;
		$blog->comment = 1;
		$blog->last_comment_timestamp = 1216807999;
		$blog->last_comment_name = 'haict';
		$blog->last_comment_uid = 58254;
		$blog->comment_count = 100;
		$blog->link_id = 0;
		
		for($i=0; $i<20;$i++){
			$blog->nid = $blog->nid+$i;
			$ret[] = $blog;
		}
	
		return $ret;
	}
	
	private function clearEventBlogCache($eid){
		$this->deleteCache("html_event_blog_block_".$eid);
	}
}

